package com.example.uit_management;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class Activity_list_shedule_semester extends Activity {

	private JsonParser_course Json_course=new JsonParser_course();
	private JsonParser_Faculty Json_faculty=new JsonParser_Faculty();
	private JsonParser_Semester Json_semester=new JsonParser_Semester();
	private ListView list;	
	private ArrayAdapterSchedule_Semester adapter;	
	private HashMap<String, String> map = new HashMap<String, String>();
	private HashMap<String, String> map_faculty = new HashMap<String, String>();
	private ArrayList<HashMap<String, String>> array_json_course=new ArrayList<HashMap<String, String>>();
	private ArrayList<HashMap<String, String>> array_json_semester=new ArrayList<HashMap<String, String>>();
	private ArrayList<HashMap<String, String>> array_json_faculty=new ArrayList<HashMap<String, String>>();
	private ArrayList<HashMap<String, String>> array_json_stemp=new ArrayList<HashMap<String, String>>();
	//private ArrayList<HashMap<String, String>> array_MAP=new ArrayList<HashMap<String, String>>();
	////khai bao bien cho spinner
	/** spinner */
	private Spinner spinner;	
	/** Mang khoa hoc dang ki */
	ArrayList<String> ArrayList ;
	private ArrayAdapter spinnerArrayAdapter;	
	/** Nien khoa **/
	private static String Spinner_semester="";
////khai bao bien cho spinner
	/** spinner */
	private Spinner spinner_faculty;	
	/** Mang khoa hoc dang ki */
	ArrayList<String> ArrayList_faculty ;
	private ArrayAdapter spinnerArrayAdapter_faculty;	
	/** Nien khoa **/
	private static String Spinner_semester_faculty="";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_shedule_semester);
        Menu_GridView.dialog.dismiss();
        Log.e(Activity_list_shedule_semester.class.toString(), "Vao schedule "); 
        
		/////////////////////////////
		// Spriner
		///////////////////////////
        try{ 
		
		/////////////////////////////
		// Spriner
		///////////////////////////		
		// set semetester for spinner
		ArrayList_faculty = new ArrayList<String>();
		ArrayList_faculty.clear();
		map_faculty.clear();
		array_json_faculty=Json_faculty.getMap_Faculty();
		//Collections.sort(array_json_semester, new ArrayMap_schedule_semeter()); 
		for (int i = 0; i < array_json_faculty.size(); i++) {
		ArrayList_faculty.add(array_json_faculty.get(i).get(JsonParser_Faculty.KEY_facultyName));
		map_faculty.put(array_json_faculty.get(i).get(JsonParser_Faculty.KEY_facultyName),array_json_faculty.get(i).get(JsonParser_Faculty.KEY_facultyID));
		Log.e(Activity_list_shedule_semester.class.toString(), "Vao schedule 2 "); 
		}
		Spinner_semester_faculty="";
		spinner_faculty = (Spinner) findViewById(R.id.spinner_schedule_semester_faculyty);
		spinnerArrayAdapter_faculty = new ArrayAdapter(this,android.R.layout.simple_spinner_item,ArrayList_faculty);
		spinner_faculty.setAdapter(spinnerArrayAdapter_faculty);		
		spinner_faculty.setOnItemSelectedListener(Spinner_ItemSelectedListener_shedule_semester);
		if(ArrayList_faculty.size()>0){
		spinner_faculty.setSelection(ArrayList_faculty.size()-1);
		}  
		Spinner_semester_faculty=spinner_faculty.getSelectedItem().toString();
        
		// set semetester for spinner
        ArrayList = new ArrayList<String>();
        ArrayList.clear();
        map.clear();
        array_json_semester=Json_semester.getMap();
        Collections.sort(array_json_semester, new ArrayMap_schedule_semeter()); 
        for (int i = 0; i < array_json_semester.size(); i++) {
    		ArrayList.add("Học kì:"+array_json_semester.get(i).get(JsonParser_Semester.KEY_semesterIndex)+"_Năm :"+array_json_semester.get(i).get(JsonParser_Semester.KEY_semesterYear));
    		Log.e(Activity_list_shedule_semester.class.toString(), "Vao schedule 1 "); 
    		map.put("Học kì:"+array_json_semester.get(i).get(JsonParser_Semester.KEY_semesterIndex)+"_Năm :"+array_json_semester.get(i).get(JsonParser_Semester.KEY_semesterYear), array_json_semester.get(i).get(JsonParser_Semester.KEY_name));
    		Log.e(Activity_list_shedule_semester.class.toString(), "Vao schedule 2 "); 
        }
		Spinner_semester="";
		spinner = (Spinner) findViewById(R.id.spinner_schedule_semester);
		spinnerArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,ArrayList);
		spinner.setAdapter(spinnerArrayAdapter);		
		spinner.setOnItemSelectedListener(Spinner_ItemSelectedListener_shedule);
		if(ArrayList.size()>0){
			spinner.setSelection(ArrayList.size()-1);
		}
        }catch(Exception e){
        	e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity__score, menu);
        return true;
    }  
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_back:{
        	finish();  
        	Intent intent = new Intent(getApplicationContext(),Menu_GridView.class);
				startActivity(intent);				
				break;
        	}
        }
        return true;
    }
    /**
     *Ham thong bao voi noi dung Day_of_week
     * */
    public void Show_Alert(String Day_of_week){
   	 AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(Activity_list_shedule_semester.this);
   	 myAlertDialog.setTitle("MÔN HỌC THỨ "+MainActivity.day);
   	 if(Day_of_week=="")  	 {
   		 myAlertDialog.setMessage("HÔM NAY KHÔNG CÓ HỌC");
   	 }else
   		 myAlertDialog.setMessage(Day_of_week);

   	 myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

   	  public void onClick(DialogInterface arg0, int arg1) {
   	  // do something when the OK button is clicked
   	  }});   	
   	 myAlertDialog.show();
   }
    static public String getSemester_Spinner(){
    	return Spinner_semester;
    }
    private OnItemSelectedListener Spinner_ItemSelectedListener_shedule = new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            // your code here
        	// khi thay doi staue        	          
            Spinner_semester=spinner.getSelectedItem().toString();
        	Log.e(Activity_list_shedule_semester.class.toString(),"SPinner chose select :" +spinner.getSelectedItem().toString());
        	// set lai custom list         	
        	if(Spinner_semester !=""){  
            	Log.e(Activity_list_shedule_semester.class.toString(),map.get(Spinner_semester));

		        	array_json_course= Json_course.getMap_Semester(map.get(Spinner_semester));
		        	array_json_stemp.clear();
		        	Log.e(Activity_list_shedule_semester.class.toString(),"faculyty 2.2");
		        	for (int i = 0; i < array_json_course.size(); i++) {
		        		Log.e(Activity_list_shedule_semester.class.toString(),"faculyty 2.3 "+ array_json_course.get(i).get(JsonParser_course.KEY_facultyID));
						if(array_json_course.get(i).get(JsonParser_course.KEY_facultyID).equals(map_faculty.get(Spinner_semester_faculty)))
						{
							Log.e(Activity_list_shedule_semester.class.toString(),"faculyty 2");
							array_json_stemp.add(array_json_course.get(i));
						}
					}
		        	list=(ListView)findViewById(R.id.list_schedule_semester);
		        	adapter =new ArrayAdapterSchedule_Semester(Activity_list_shedule_semester.this, array_json_stemp);
		        	list.setAdapter(adapter);     		
        		}
        	}
        @Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub			
		}        	
        };  
        private OnItemSelectedListener Spinner_ItemSelectedListener_shedule_semester = new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                // your code here
            	// khi thay doi staue        	          
                Spinner_semester_faculty=spinner_faculty.getSelectedItem().toString();
            	Log.e(Activity_list_shedule_semester.class.toString(),"SPinner chose select :" +spinner_faculty.getSelectedItem().toString());
            	// set lai custom list 
            	if(Spinner_semester_faculty !=""){  
                	Log.e(Activity_list_shedule_semester.class.toString(),"faculyty "+map_faculty.get(Spinner_semester_faculty));

    		        	array_json_course= Json_course.getMap_Semester(map.get(Spinner_semester));
    		        	array_json_stemp.clear();
	                	Log.e(Activity_list_shedule_semester.class.toString(),"faculyty 1.2");

    		        	for (int i = 0; i < array_json_course.size(); i++) {
    						if(array_json_course.get(i).get(JsonParser_course.KEY_facultyID).equals(map_faculty.get(Spinner_semester_faculty)))
    						{
    		                	Log.e(Activity_list_shedule_semester.class.toString(),"faculyty 1");
    							array_json_stemp.add(array_json_course.get(i));
    						}
    					}
    		        	list=(ListView)findViewById(R.id.list_schedule_semester);
    		        	adapter =new ArrayAdapterSchedule_Semester(Activity_list_shedule_semester.this, array_json_stemp);
    		        	list.setAdapter(adapter);       		
            		}
            	}
            @Override
    		public void onNothingSelected(AdapterView<?> arg0) {
    			// TODO Auto-generated method stub			
    		}        	
            };  
}

